package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private MyPriciousMapper myPriciousMapper;
    @Autowired
    private MyCreditMapper myCreditMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 功能:
     * 1.注册时: 检查email注册账号是否存在
     * 2.登录时: 检查是否存在该用户名和密码
     * 两个用法
     * @return 返回从数据库查询出来的结果
     */
    @Override
    public User getUserByEmailAndPassword(String email, String password) {
        User user = userMapper.selectUserByEmailAndPassword(email, password);
       return user;
    }

    @Override
    public void save(User user) {
        userMapper.insert(user);
    }

    @Override
    public User regist(User user) {
        User temp = userMapper.selectUserByEmailAndPassword(user.getEmail(), null);
        if (temp != null) {
            throw new RuntimeException("该邮箱号已经被注册");
        }
        //1.设置默认头像
        user.setHeadImgUrl("/img/user/head.jpg");
        //2.设置默认背景墙
        user.setCoverImgUrl("/img/user/bg.jpeg");
        userMapper.insert(user);
        return user;

    }

    @Override
    public User login(String email, String password) {
        User user = userMapper.selectUserByEmailAndPassword(email, password);
        if (user == null) {
            throw new RuntimeException("邮箱或密码错误");
        }
        return user;
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public User get(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public MyCredit selectCredits(Long userId) {
        return myCreditMapper.selectByPrimaryKey(userId);
    }

    @Override
    public void spendCredits(MyCredit myCredit) {
        myCreditMapper.spendCredits(myCredit);
    }

    @Override
    public List<Map<String, Object>> selectShoppingCart(Long userId) {
        return userMapper.selectShoppingCart(userId);
    }

    @Override
    public void addProducts(ShoppingCart shoppingCart) {
        ShoppingCart cart = shoppingCartMapper.selectByUserIdAndProductId(shoppingCart);
        if (cart == null) {
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public User getUserByTargetId(Long targetUserId) {
        return null;
    }

    @Override
    public void addPricious(MyPricious myPricious) {

    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public void deleteProducts(ShoppingCart shoppingCart) {
        shoppingCartMapper.delete(shoppingCart);
    }

    @Override
    public void addOrder(Order order) {
        orderMapper.insert(order);
    }

    @Override
    public List<Map<String, Object>> selectOrder(Long userId) {
        Date closeTime = orderMapper.selectCloseTime(userId);
        return orderMapper.selectByOrderNum(userId, closeTime);
    }
}
